package com.pangda.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pangda.common.domain.PageResult;
import com.pangda.common.domain.ResponseResult;
import com.pangda.common.utils.FileUtil;
import com.pangda.common.utils.UserContext;
import com.pangda.pojo.SysRole;
import com.pangda.pojo.data.SysRoleData;
import com.pangda.pojo.dto.SysRoleDto;
import com.pangda.pojo.vo.SysRoleVo;
import com.pangda.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author xjy
 * @since 2024-06-20
 */
@RestController
@RequestMapping("/system/role")
@Api(tags = "角色管理相关接口")
public class SysRoleController {

    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping("/list")
    @ApiOperation("获取角色列表")
    public PageResult<SysRoleVo> list(PageResult pageResult, SysRoleDto sysRoleDto) {
        return roleService.roleList(pageResult, sysRoleDto);
    }

    @PostMapping
    @ApiOperation("新增角色")
    public ResponseResult add(@RequestBody SysRoleDto sysRoleDto) {
        roleService.addRole(sysRoleDto);
        return ResponseResult.success();
    }

    @GetMapping("/{roleId}")
    @ApiOperation("获取角色详情")
    public ResponseResult<SysRoleVo> get(@PathVariable Long roleId) {
        return roleService.getRole(roleId);
    }

    @PutMapping
    @ApiOperation("修改角色")
    public ResponseResult update(@RequestBody SysRoleDto sysRoleDto) {
        return roleService.updateRole(sysRoleDto);
    }

    @PutMapping("/changeStatus")
    @ApiOperation("修改角色状态")
    public ResponseResult changeStatus(@RequestBody SysRoleDto sysRoleDto) {
        if (ObjectUtil.isNotEmpty(sysRoleDto.getRoleId())) {
            LambdaUpdateWrapper<SysRole> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(SysRole::getRoleId, sysRoleDto.getRoleId())
                    .set(SysRole::getStatus, sysRoleDto.getStatus());
            roleService.update(BeanUtil.toBean(sysRoleDto, SysRole.class), wrapper);
            return ResponseResult.success();
        }
        return ResponseResult.error();
    }

    @PutMapping("/dataScope")
    @ApiOperation("修改角色数据权限")
    public ResponseResult dataScope(@RequestBody SysRoleDto sysRoleDto) {
        if (ObjectUtil.isNotEmpty(sysRoleDto.getRoleId())) {
            LambdaUpdateWrapper<SysRole> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(SysRole::getRoleId, sysRoleDto.getRoleId())
                    .set(SysRole::getDataScope, sysRoleDto.getDataScope());
            roleService.update(BeanUtil.toBean(sysRoleDto, SysRole.class), wrapper);
            return ResponseResult.success();
        }
        return ResponseResult.error();
    }

    @DeleteMapping("/{roleIds}")
    @ApiOperation("删除角色")
    public ResponseResult remove(@PathVariable List<Integer> roleIds) {
        if (ObjectUtil.isEmpty(roleIds)) {
            return ResponseResult.error();
        }
        LambdaUpdateWrapper<SysRole> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(SysRole::getRoleId, roleIds)
                .set(SysRole::getDelFlag, 1);
        roleService.update(wrapper);
        return ResponseResult.success();
    }

/*    @GetMapping("/optionselect")
    @ApiOperation("获取角色下拉列表")
    public ResponseResult<List<SysRoleVo>> optionselect() {
        return null;
    }*/


    @GetMapping("/export")
    @ApiOperation("导出角色数据")
    public Map<String, String> export(PageResult pageResult, SysRoleDto sysRoleDto) throws IOException {
        PageResult<SysRoleVo> sysRoleVoPageResult = roleService.roleList(pageResult, sysRoleDto);
        List<SysRoleVo> rows = sysRoleVoPageResult.getRows();
        List<SysRoleData> sysRoleData = BeanUtil.copyToList(rows, SysRoleData.class);
        Map<String, String> result = FileUtil.uploadFile(sysRoleData, SysRoleData.class, redisTemplate);
        return result;
    }

}
